﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

Partial Class Reports_report_stp_auth_department2
    Inherits System.Web.UI.Page
    Private report As New CrystalDecisions.CrystalReports.Engine.ReportDocument
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        CrystalReportViewer1.ReportSource = report
        CrystalReportViewer1.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX
        report.PrintOptions.PaperOrientation = PaperOrientation.Landscape
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dw As report_stp_auth_departmentTableAdapters.USP_List_stp_auth_department1TableAdapter
        Dim dw2 As report_stp_auth_departmentTableAdapters.USP_List_stp_auth_getTableAdapter
        Dim dt As DataTable
        Dim dt2 As DataTable
        Dim dtTemp As DataTable
        Dim iDepartmentCode As Integer
        Dim iMainSystemCode As Integer
        Dim iMenuSystemCode As Integer
        Dim iDepartmentStatus As Integer
        Dim strDepartmentCode As String
        Dim strMainSystemCode As String
        Dim strMenuSystemCode As String
        Dim strDepartmentStatus As String
        Dim iStaffCode As Integer
        Dim iColumn As Integer
        Dim totalColumn As Integer
        Dim iCount As Integer = 1
        Try
            dtTemp = New DataTable
            'get Querystring
            iDepartmentCode = Request.QueryString("department_code")
            iMainSystemCode = Request.QueryString("main_system_code")
            iMenuSystemCode = Request.QueryString("menu_system_code")
            iDepartmentStatus = Request.QueryString("department_status")

            strDepartmentCode = Request.QueryString("str_department_code")
            strMainSystemCode = Request.QueryString("str_main_system_code")
            strMenuSystemCode = Request.QueryString("str_menu_system_code")
            strDepartmentStatus = Request.QueryString("str_department_status")
            iStaffCode = Session("staff_code")

            'check status , 0: ทั้งหมด  ,1: ใช้ , 2: ไม่ใช้
            strDepartmentCode = IIf(strDepartmentCode = "หน่วยงาน", "ทั้งหมด", strDepartmentCode)
            strMainSystemCode = IIf(strMainSystemCode = "ระบบงาน", "ทั้งหมด", strMainSystemCode)
            strMenuSystemCode = IIf(strMenuSystemCode = "เมนูระบบงาน", "ทั้งหมด", strMenuSystemCode)
            strDepartmentStatus = IIf(strDepartmentStatus = "สถานะ", "ทั้งหมด", strDepartmentStatus)

            dw = New report_stp_auth_departmentTableAdapters.USP_List_stp_auth_department1TableAdapter
            dw2 = New report_stp_auth_departmentTableAdapters.USP_List_stp_auth_getTableAdapter
            'Retrieve data from DB & collect to datatable
            dt = dw.GetData(iStaffCode, iDepartmentCode, iDepartmentStatus, iMainSystemCode, iMenuSystemCode)

            dt2 = dw2.GetData()

            dtTemp = GenTable(dt, dt2)
            ' dtTemp.TableName = "AuthenTable"
            'Get no. of authen
            iColumn = dt2.Rows.Count
            'check 
            If (dt.Rows.Count > 0 And iColumn > 0) Then
                'Create report engine
                ' Dim report As New CrystalDecisions.CrystalReports.Engine.ReportDocument
                'If iColumn <= 7 Then
                '    'map report parth
                '    report.Load(Server.MapPath("report_stp_auth_department.rpt"))
                '    totalColumn = 7
                'Else
                report.Load(Server.MapPath("report_stp_auth_department2.rpt"))
                totalColumn = 15
                ' End If
                'set data table to report
                report.SetDataSource(dtTemp)

                'set input paratmenter (default)
                report.SetParameterValue("DepartmentCode", strDepartmentCode)
                report.SetParameterValue("MainCode", strMainSystemCode)
                report.SetParameterValue("MenuCode", strMenuSystemCode)
                report.SetParameterValue("Status", strDepartmentStatus)

                Dim remainColumn As Integer
                For Each dr As DataRow In dt2.Rows
                    report.SetParameterValue("param" + iCount.ToString(), dr("type_name"))
                    iCount += 1
                Next

                remainColumn = totalColumn - iCount + 1

                For index As Integer = 1 To remainColumn
                    report.SetParameterValue("param" + (iCount).ToString, "")
                    iCount += 1
                Next


                'hidden line
                hiddenLine(report, iColumn)

                'Show report
                ' CrystalReportViewer1.ReportSource = report
            Else
                ' hide invalid message
                CrystalReportViewer1.Visible = False

                GenericHelper.ShowAlert(Me, "ไม่พบข้อมูล")
                Dim strscript As String = "<script language=javascript>window.top.close();</script>"
                If (Not Page.IsStartupScriptRegistered("clientScript")) Then
                    Page.RegisterStartupScript("clientScript", strscript)
                End If
                ' TODO:
                ' Redirect to pervious page
            End If
        Catch ex As AccessViolationException
            ' hide invalid message
            CrystalReportViewer1.Visible = False

            GenericHelper.ShowAlert(Me, ex.Message)
            Dim strscript As String = "<script language=javascript>window.top.close();</script>"
            If (Not Page.IsStartupScriptRegistered("clientScript")) Then
                Page.RegisterStartupScript("clientScript", strscript)
            End If
        Finally
            If Not (dt Is Nothing) Then
                dt.Dispose()
            End If
            If Not (dw Is Nothing) Then
                dw.Dispose()
            End If
        End Try
    End Sub

    Private Sub hiddenLine(ByVal report As CrystalDecisions.CrystalReports.Engine.ReportDocument, ByVal Column As Integer)
        Dim Line2, Line5, Line3 As LineObject
        Dim text13, text11 As TextObject
        Dim txtHeader As TextObject = report.ReportDefinition.ReportObjects("txtHeader")
        Dim txt34 As TextObject
        Dim lineHeader_M, lineHeader_T, lineHeader_B As LineObject

        lineHeader_M = report.ReportDefinition.ReportObjects("LineHeader_M")
        lineHeader_T = report.ReportDefinition.ReportObjects("LineHeader_T")
        lineHeader_B = report.ReportDefinition.ReportObjects("LineHeader_B")
        txt34 = report.ReportDefinition.ReportObjects("Text34")

        For i As Integer = Column + 1 To 12
            setNoLine("Line" + CStr(i) + "_H", report)
            setNoLine("Line" + CStr(i) + "_T", report)
            setNoLine("Line" + CStr(i) + "_B", report)
        Next


        For j As Integer = 1 To Column - 1
            setNoLine("Line" + CStr(j) + "_H", report)
        Next

        If Column = 1 Then
            txtHeader.Width = 885
            lineHeader_T.Right = 7245
            lineHeader_B.Right = 7247
            lineHeader_M.Right = 7247
            txt34.Width = 4015
        ElseIf Column = 2 Then
            txtHeader.Width = 1740
            lineHeader_T.Right = 8100
            lineHeader_B.Right = 8100
            lineHeader_M.Right = 8100
            txt34.Width = 4875
        ElseIf Column = 3 Then
            txtHeader.Width = 2618
            lineHeader_T.Right = 8985
            lineHeader_B.Right = 8985
            lineHeader_M.Right = 8985
            txt34.Width = 5750

        ElseIf Column = 4 Then
            txtHeader.Width = 3488
            lineHeader_T.Right = 9845
            lineHeader_B.Right = 9845
            lineHeader_M.Right = 9845
            txt34.Width = 6615

        ElseIf Column = 5 Then
            txtHeader.Width = 4328

            lineHeader_T.Right = 10710
            lineHeader_B.Right = 10710
            lineHeader_M.Right = 10710
            txt34.Width = 7480
        ElseIf Column = 6 Then
            txtHeader.Width = 5175

            lineHeader_T.Right = 11565
            lineHeader_B.Right = 11565
            lineHeader_M.Right = 11565
            txt34.Width = 8340

        ElseIf Column = 7 Then
            txtHeader.Width = 6075

            lineHeader_T.Right = 12465
            lineHeader_B.Right = 12465
            lineHeader_M.Right = 12465
            txt34.Width = 9235

        ElseIf Column = 8 Then


            lineHeader_M.Right = 13330
            lineHeader_T.Right = 13330
            lineHeader_B.Right = 13330

            txt34.Width = 10100
            txtHeader.Width = 6830

        ElseIf Column = 9 Then

            'line
            lineHeader_M.Right = 14195
            lineHeader_T.Right = 14195
            lineHeader_B.Right = 14195
            txt34.Width = 10980
 
            txtHeader.Width = 6830 + 825

        ElseIf Column = 10 Then

            lineHeader_M.Right = 15055
            lineHeader_T.Right = 15055
            lineHeader_B.Right = 15055
            txt34.Width = 11823

            txtHeader.Width = 6830 + (2 * 825)

        ElseIf Column = 11 Then
 
            lineHeader_M.Right = 15920
            lineHeader_T.Right = 15920
            lineHeader_B.Right = 15920
            txt34.Width = 12685
            txtHeader.Width = 6830 + (3 * 825)
        Else
            lineHeader_M.Right = 16780
            lineHeader_T.Right = 16780
            lineHeader_B.Right = 16780
            txt34.Width = 13553

            txtHeader.Width = 6830 + (4 * 825)
        End If

    End Sub

    Private Function GenTable(ByVal dtMainValue As DataTable, ByVal dtAuth As DataTable) As DataTable
        Dim dt As New DataTable
        Dim ar As New ArrayList
        dt.Columns.Add("seqno")
        dt.Columns.Add("deparment")
        dt.Columns.Add("systemname")
        dt.Columns.Add("menuname")

        For i As Integer = 1 To 15
            dt.Columns.Add("auth" + i.ToString)
        Next

        Dim iCount As Integer = 1
        For Each dr As DataRow In dtAuth.Rows
            ar.Add(dr("type_name"))
            iCount += 1
        Next

        Dim dRowTemp As DataRow
        Dim strDepartment As String = ""
        Dim mainSystem As String = ""
        Dim menuSystem As String = ""
        Dim iRows As Integer = 1


        For Each drow As DataRow In dtMainValue.Rows
            If (strDepartment <> drow("dept_hirachy") OrElse mainSystem <> drow("main_system_name") OrElse menuSystem <> drow("menu_system_name")) Then
                dRowTemp = dt.NewRow

                strDepartment = drow("dept_hirachy")
                mainSystem = drow("main_system_name")
                menuSystem = drow("menu_system_name")

                dRowTemp("seqno") = iRows.ToString()
                dRowTemp("deparment") = strDepartment
                dRowTemp("systemname") = mainSystem
                dRowTemp("menuname") = menuSystem
                dRowTemp("auth" + (ar.IndexOf(drow("type_name")) + 1).ToString) = drow("type_name")
                iRows += 1
                'Add New Row
                dt.Rows.Add(dRowTemp)
            Else
                dRowTemp("auth" + (ar.IndexOf(drow("type_name")) + 1).ToString) = drow("type_name")
            End If
        Next

        If Not (dRowTemp Is Nothing) Then

            If (dt.Rows.IndexOf(dRowTemp) < 0) Then
                dt.Rows.Add(dRowTemp)
            End If
        End If

        Return dt

    End Function

    Private Sub setNoLine(ByVal LineName As String, ByVal rDoc As CrystalDecisions.CrystalReports.Engine.ReportDocument)
        Dim objLine As LineObject = CType(rDoc.ReportDefinition.ReportObjects(LineName), LineObject)
        objLine.LineStyle = CrystalDecisions.Shared.LineStyle.NoLine
    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        If Not (CrystalReportViewer1 Is Nothing) Then
            CrystalReportViewer1.Dispose()
        End If
    End Sub

    Protected Sub CrystalReportViewer1_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles CrystalReportViewer1.Unload
        If Not (report Is Nothing) Then
            report.Close()
            report.Dispose()
        End If
    End Sub

     
End Class
